/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jclaimtest;

import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.Map;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

/**
 *
 * @author lendle
 */
public class Test1 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws Exception {
        // TODO code application logic here
        URL url = new URL("https://login.yahoo.com/WSLogin/V1/get_auth_token?&login=cherry8620&passwd=len1028&oauth_consumer_key=dj0yJmk9MDBQNUhLazNmZXNBJmQ9WVdrOU1YQkhVV2h2TjJrbWNHbzlNVEk0TlRBM05EYzJNZy0tJnM9Y29uc3VtZXJzZWNyZXQmeD03Ng--");
        BufferedReader input = new BufferedReader(new InputStreamReader(url.openStream()));
        String str = input.readLine();
        String token = null;
        if (str != null) {
            int index = str.indexOf("=");
            token = str.substring(index + 1);
        }
        input.close();
        System.out.println("token=" + token);
        long timestamp = ((long) (System.currentTimeMillis() / 1000));
        String getTokenURL = "https://api.login.yahoo.com/oauth/v2/get_token?oauth_consumer_key=dj0yJmk9MDBQNUhLazNmZXNBJmQ9WVdrOU1YQkhVV2h2TjJrbWNHbzlNVEk0TlRBM05EYzJNZy0tJnM9Y29uc3VtZXJzZWNyZXQmeD03Ng--&"
                + "oauth_signature_method=PLAINTEXT"
                + "&oauth_nonce=24829.2331&"
                + "oauth_timestamp=" + timestamp + "&"
                + "oauth_signature=583e9113b3f1cbe6d11a98de4e8f144f1be5dfcf%26&"
                + "oauth_version=1.0&"
                + "oauth_token=" + token + "&"
                + "realm=yahooapis.com&"
                + "oauth_callback=oob";
        System.out.println(getTokenURL);
        url = new URL(getTokenURL);
        input = new BufferedReader(new InputStreamReader(url.openStream()));
        str = input.readLine();
        String oauthToken = null;
        String oauthSecret = null;
        if (str != null) {
            int index = str.indexOf("=");
            int index1 = str.indexOf("&", index + 1);
            int index2 = str.indexOf("=", index1 + 1);
            int index3 = str.indexOf("&", index2 + 1);
            oauthToken = str.substring(index + 1, index1);
            oauthSecret = str.substring(index2 + 1, index3);
        }
        input.close();
        System.out.println("oauthToken=" + oauthToken);
        System.out.println("oauthSecret=" + oauthSecret);


        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost httpost = new HttpPost("http://developer.messenger.yahooapis.com/v1/session");
        httpost.setHeader("Content-Type", "application/json;charset=utf-8");
        String oauthCredential = "OAuth realm=\"yahooapis.com\","
                + "oauth_consumer_key=\"dj0yJmk9MDBQNUhLazNmZXNBJmQ9WVdrOU1YQkhVV2h2TjJrbWNHbzlNVEk0TlRBM05EYzJNZy0tJnM9Y29uc3VtZXJzZWNyZXQmeD03Ng--\","
                + "oauth_nonce=\"24829.2330\","
                + "oauth_signature_method=\"PLAINTEXT\","
                + "oauth_timestamp=\"" + timestamp + "\","
                + "oauth_token=\"" + oauthToken + "\","
                + "oauth_version=\"1.0\","
                + "oauth_signature=\"583e9113b3f1cbe6d11a98de4e8f144f1be5dfcf%26" + oauthSecret + "\"";
        httpost.setHeader("Authorization", oauthCredential);
        //ArrayList<NameValuePair> pairList = new ArrayList<NameValuePair>();
        //pairList.add(new BasicNameValuePair("Authorization", oauthToken));
        //pairList.add(new BasicNameValuePair("oauth_signature", "583e9113b3f1cbe6d11a98de4e8f144f1be5dfcf%26"));
        //StringEntity entity = new StringEntity(URLEncodedUtils.format(pairList, "UTF-8"));

        StringEntity entity = new StringEntity("{}");
        httpost.setEntity(entity);
        HttpResponse response = httpClient.execute(httpost);
        String responseString = EntityUtils.toString(response.getEntity());
        System.out.println(responseString);
        ObjectMapper mapper = new ObjectMapper();
        Map map = mapper.readValue(responseString, Map.class);
        String sessionId = (String) map.get("sessionId");

        httpost = new HttpPost("http://rcore1.messenger.yahooapis.com/v1/message/yahoo/cherry8620?sid=" + sessionId);
        httpost.setHeader("Content-Type", "application/json;charset=UTF-8");
        httpost.setHeader("Authorization", oauthCredential);
        //String message=URLEncoder.encode("I'm still testing :)) Ok, get reset earlier, good night!", "utf-8");
        String message = "中文 123";
        entity = new StringEntity("{\"message\" : \"" + message + "\"}", "utf-8");
        httpost.setEntity(entity);
        response = httpClient.execute(httpost);
        responseString = EntityUtils.toString(response.getEntity());
        System.out.println(responseString);
//        Session session = new Session(new HTTPConnectionHandler("scsc.msg.yahoo.com", 5050));
//        //Session session = new Session(new SOCKSConnectionHandler("80.167.238.77", 1080));
//        System.out.println("attempt login");
//        session.login("cherry8620", "len1028");
//        System.err.println("logged in");
//        session.logout();
    }
}
